/* Copyright 2009 Palm, Inc.  All rights reserved. */

ContactPointDecorator = {};

ContactPointDecorator.phoneFormatter = function(ph){
//    if (ph.hasBeenFormatted) {
//        return ph;
//    }
    var phone = Object.extend(ph, new PhoneNumber(ph.value));
    if(phone.label === undefined){
		phone.label = 3;
	}
	
	ContactPointDecorator.formatContactPoint(phone, PhoneNumber.labels);
	
    phone.type = "phone";
    if (phone.quickDial) {
        phone.hasQuickDial = "show-quick-dial";
        phone.size = "assigned";
    }
    else {
        phone.size = "unassigned";
    }
    phone.hasBeenFormatted = true;
    return phone;
};

ContactPointDecorator.emailFormatter = function(pt){
//    if (pt.hasBeenFormatted) {
//        return pt;
//    }
    ContactPointDecorator.formatContactPoint(pt);
	pt.displayValue = pt.value;
    pt.type = "email";
    return pt;
};

ContactPointDecorator.messagingFormatter = function(im){
//    if (im.hasBeenFormatted) {
//        return im;
//    }
    ContactPointDecorator.formatContactPoint(im, IMName.labels);
	im.displayValue = im.value || im.displayValue;
    im.showPresence = true;
    
    if (im.showPresence) {
        switch (im.availability) {
            case IMName.BUSY:
                im.statusImage = 'status-busy';
                break;
            case IMName.IDLE:
                im.statusImage = 'status-idle';
                break;
            case IMName.ONLINE:
                im.statusImage = 'status-available';
                break;
			case IMName.OFFLINE:
				im.statusImage = 'status-offline';
				break;
            default:
                im.statusImage = '';
        }
    }
    
    im.type = "im";
    
    if (im.customMessage) {
		im.customMessage = im.customMessage.unescapeHTML();
		im.customMessage = im.customMessage.replace(/&apos;/g, "'").replace(/&quot;/g, "\"");
		im.showCustomMessage = 'show-custom-message';
	}
    im.hasBeenFormatted = true;
    return im;
};

ContactPointDecorator.addressFormatter = function(addr){
//    if (addr.hasBeenFormatted) {
//        return addr;
//    }
    //TODO This could be simpler and faster, see exactly what this is doing
    Object.extend(addr, new Address(addr));
    addr.parseAddress(addr.freeformAddress);
    ContactPointDecorator.formatContactPoint(addr, Address.labels);
    if (!(addr.city || addr.state || addr.zipCode)) {
        addr.showMore = "none";
    }
    addr.freeformAddress = addr.getOneLine();
	addr.displayValue = addr.freeformAddress.escapeHTML().gsub("\n", "<br />");
    addr.type = "address";
    addr.hasBeenFormatted = true;
    return addr;
};

ContactPointDecorator.urlFormatter = function(url){
//    if (url.hasBeenFormatted) {
//        return url;
//    }
    url.type = "web";
    if (url.url && url.url.indexOf("http") !== 0) {
        url.url = "http://" + url.url;
    }
    url.hasBeenFormatted = true;
    return url;
};

ContactPointDecorator.formatContactPoint = function(pt, labels){
	labels = labels || ContactPointLabels;
    //If there's a custom label display that, if there's not, display the defaults.
    pt.displayLabel = pt.customLabel || labels[pt.label] || labels[pt.serviceName] || $L("Other");
    pt.type = "other";
    return pt;
};
